//
//  SearchMerchantParser.m
//  FootPrint
//
//  Created by 秦 东方 on 4/14/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "SearchMerchantParser.h"
#import "MerchantPropertyRequest.pb.h"
#import "MerchantQueryRequest.pb.h"
#import "ASIDataProvider.h"

@implementation SearchMerchantParser

- (void)requestFilter:(long long)uid cityName:(NSString*)city subCity:(NSString*)district busiCircle:(NSString*)circle type:(NSString*)type{

    MerchantPropertyRequestMessage_Builder* builder = [MerchantPropertyRequestMessage builder];
    [builder setUserId:uid];
    if(nil != city && city.length > 0){
        [builder setCity:city];
    }
    if(nil != district && district.length > 0){
        [builder setDistrict:district];
    }
    if(nil != circle && circle.length > 0){
        [builder setBusinessCircle:circle];
    }
    if(nil != type && type.length > 0){
        [builder setType:type];
    }
    NSData* queryData = [[builder build] data];
    if(queryData){
        
        [super noCacheRequestURL:URL_MERCHANT_FILTER postData:queryData completion:^(NSData* resultData){
            @try {
                MerchantPropertyResponseMessage* resp = [MerchantPropertyResponseMessage parseFromData:resultData];
                [_delegate didFinishReqFilter:resp];
            }
            @catch (NSException *exception) {
                [_delegate failReqFilter];
            }
            
        } fail:^(NSError * err) {
            [_delegate failReqFilter];
        }];
        
    } else {
        [_delegate failReqFilter];
    }

}


- (void)requestMerchant:(long long)uid cityName:(NSString*)city districtName:(NSString*)district busiCircle:(NSString*)circle type:(NSString*)type subType:(NSString*)subType startIndex:(int)startIndex filter:(NSString*)filter{
    
    MerchantQueryRequestMessage_Builder* builder = [MerchantQueryRequestMessage builder];
    [builder setUserId:uid];
    
    if(nil != city && city.length > 0){
        [builder setCity:city];
    }
    
    if(nil != district && district.length > 0){
        [builder setDistrict:district];
    }
    
    if(nil != circle && circle.length > 0){
        [builder setBusinessCircle:circle];
    }
    
    if(nil != type && type.length > 0){
        [builder setType:type];
    }
    
    if(nil != subType && subType.length > 0){
        [builder setSubType:subType];
    }
    
    if(nil != filter && filter.length > 0){
        [builder setMerchantName:filter];
    }
    [builder setStartCur:startIndex];
    
    NSData* queryData = [[builder build] data];
    if(queryData){
        
        [super noCacheRequestURL:URL_MERCHANT postData:queryData completion:^(NSData* resultData){
            @try {
                MerchantQueryResponseMessage* resp = [MerchantQueryResponseMessage parseFromData:resultData];
                [_delegate didfinishReqMer:resp];
            }
            @catch (NSException *exception) {
                [_delegate failReqMer];
            }
            
        } fail:^(NSError * err) {
            [_delegate failReqMer];
        }];
        
    } else {
        [_delegate failReqMer];
    }
    
}


@end
